package com.jbit.examSystem.service.impl;

import com.jbit.examSystem.dao.ExamResultDao;
import com.jbit.examSystem.pojo.Class;
import com.jbit.examSystem.pojo.ClassInfo;
import com.jbit.examSystem.pojo.GradeInfo;
import com.jbit.examSystem.service.ExamResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ExamResultServiceImpl implements ExamResultService {
    @Autowired
    private ExamResultDao examResultDao;

    @Override
    public List<GradeInfo> getInfo(Integer gradeId) {
        List<Class> classes = examResultDao.getAllClass(gradeId);
        List<GradeInfo> infos = new ArrayList<>();

        try {
            for (Class aClass : classes) {
                GradeInfo info = examResultDao.getInfo(aClass.getId());
                GradeInfo info2 = examResultDao.getInfo2(aClass.getId());
                info.setClassId(info2.getClassId());
                info.setClassName(info2.getClassName());
                info.setTeacherName(info2.getTeacherName());

                String s = info.getAccuracy();
                s = s.substring(0,4);
                s = s+"%";
                info.setAccuracy(s);
                infos.add(info);
            }
        }catch (Exception e){
            return infos;
        }

        return infos;
    }

    @Override
    public List<ClassInfo> getInfo2(Integer classId) {
        List<ClassInfo> infos = examResultDao.getInfo3(classId);
        for (ClassInfo info : infos) {
            String a = info.getAccuracy();
            a = a.substring(0, 4);
            a = a+"%";
            info.setAccuracy(a);
        }
        return infos;
    }
}
